package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.dao.SaleoutstoredDao;
import io.renren.dao.SaleoutstoremDao;
import io.renren.dao.UcmlContactDao;
import io.renren.dao.UcmlOrganizeDao;
import io.renren.dto.SaleDetailDto;
import io.renren.dto.SaleoutstoremDTO;
import io.renren.entity.SaleoutstoredEntity;
import io.renren.entity.SaleoutstoremEntity;
import io.renren.entity.UcmlContactEntity;
import io.renren.entity.UcmlOrganizeEntity;
import io.renren.service.SaleoutstoremService;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-30
 */
@Service
public class SaleoutstoremServiceImpl extends CrudServiceImpl<SaleoutstoremDao, SaleoutstoremEntity, SaleoutstoremDTO> implements SaleoutstoremService {

    @Override
    public QueryWrapper<SaleoutstoremEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        QueryWrapper<SaleoutstoremEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        return wrapper;
    }


    @Autowired
    private UcmlOrganizeDao ucmlOrganizeDao;
    @Autowired
    private UcmlContactDao ucmlContactDao;
    @Autowired
    private SaleoutstoredDao saleoutstoredDao;

    @Override
    public Map<String, Object> saleDetail(String traderMOID, SaleDetailDto dto) {
        Map<String, Object> result = new HashMap<>();

        SaleoutstoremEntity info = baseDao.selectOne(new QueryWrapper<SaleoutstoremEntity>()
                .eq("SaleOutStoreMOID", dto.getSaleOutStoreMoId()));
        if (info != null) {

            result.put("serviceDate", DateUtils.format(info.getBilldate(), "yyyy-MM-dd HH:mm:ss"));
            UcmlOrganizeEntity organizeEntity = ucmlOrganizeDao.selectOne(new QueryWrapper<UcmlOrganizeEntity>()
                    .select("OrgName")
                    .eq("UCML_OrganizeOID", info.getOrgFk()));
            result.put("orgName", organizeEntity != null ? organizeEntity.getOrgname() : "");
            UcmlContactEntity ucmlContactEntity = ucmlContactDao.selectOne(new QueryWrapper<UcmlContactEntity>()
                    .select("PersonName")
                    .eq("UCML_CONTACTOID", info.getUcmlContactFk()));
            result.put("PersonName", ucmlContactEntity != null ? ucmlContactEntity.getPersonname() : "");
            List<Map<String, Object>> goodsList = saleoutstoredDao.selectMapListBySaleOutStoreM(dto.getSaleOutStoreMoId());
            result.put("goodsList", goodsList);
            // 商品

            result.put("giftList", goodsList);
            result.put("BusinessAmount", new BigDecimal(info.getBusinessamount()).setScale(2, BigDecimal.ROUND_HALF_UP));
            result.put("vipDiscount", new BigDecimal(info.getDiscountamount()).setScale(2, BigDecimal.ROUND_HALF_UP));
            result.put("couponDiscount", 0);
            result.put("Amount", new BigDecimal(info.getAmount()).setScale(2, BigDecimal.ROUND_HALF_UP));

        }
        return result;
    }
}